// 加载FORM表单扩展
;layui.use(['form'], function(){
  var form = layui.form()
  ,layer = layui.layer;
  
  //自定义验证规则
  form.verify({
    name: function(value){
      if(value.length < 1){
        return '媒体名称至少得1个字符';
      }
    }
    ,platform: function(value){
		var checkedTotal = 0;
		$("input[verify='platform']").each(function(index, item){
			if( $(item).is(':checked') ) {
				checkedTotal++;
			}
		});
		if( checkedTotal == 0 ) {
			return "请选择媒体平台";
		}
	}
	,domain: function(value){
		if(value.length < 1){
			return '请填写媒体地址';
		  }
	}
	,contact: function(value){
		if(value.length < 1){
			return '请填写联系人';
		}
	}
  });
  //监听提交
  form.on('submit(submit)', function(data){
  
      var formData = data.field;
      var platformData    = Tool.getCheckBox(data.field, 'platform');
      var platformLength  = platformData.length;
	  var media_id = $("input[name='id']").val();
      var name          =   formData.name;
      var domain        =   formData.domain;
      var contact       =   formData.contact;
      var tag           =   formData.tag;
      var remark        =   formData.remark;

      if(platformLength == 0) {
		layer.msg('请选择媒体平台', {icon: 5, shift: 6}, function(){});
		return false;
      }

      $.ajax({
          type:"POST",
          url:"/Media/edit",
          dataType:"json",
          data:{'id': media_id, 'name':name, 'platform':platformData, 'domain':domain, 'contact':contact, 'tag':tag, 'remark':remark},
          success: function(data){
              if(data.status == 'success') {
                  PopModel.refreshParentFrame();
              } else {
				layer.msg(data.msg, {icon: 5, shift: 6}, function(){});
              }
			return false;
          }
      });

    return false;
  });
	// 刷新表单
	form.render();
});